import { _decorator, Component, EventTouch, Input, Node, Vec3 } from 'cc';
import { Circle } from './Circle';
import { FishLoad } from './FishLoad';
import { GameScene } from './GameScene';
const { ccclass, property } = _decorator;

@ccclass('CricleLoad')
/**
 * 圈层
 */
export class CricleLoad extends Component {

    //游戏场景
    @property(Node)
    private GameScene: Node = null;

    //已经点击得圈
    public ChooseCircle: Circle[] = [];

    protected onLoad(): void {
        for (let circle of this.node.children) {
            circle.on(Input.EventType.TOUCH_START, this.TouchStart, this);
        }
    }

    /**
     * 初始化
     */
    public Init() {
        for (let circle of this.node.children) {
            circle.getComponent(Circle).UpdateCircle(-1);
        }
    }

    /**
     * 
     */
    private TouchStart(_e: EventTouch) {
        let circle: Circle = _e.currentTarget.getComponent(Circle);

        if (circle.currentIndex == -1) return;

        circle.IamChoose = !circle.IamChoose;
        let scale_num: number = circle.IamChoose ? 1.2 : 1;
        circle.node.setScale(new Vec3(scale_num, scale_num, scale_num))

        if (circle.IamChoose) { this.ChooseCircle.push(circle); }
        else {
            if (this.ChooseCircle.indexOf(circle) != -1) {
                this.ChooseCircle.splice(this.ChooseCircle.indexOf(circle), 1);
            }
        }

        if (this.ChooseCircle.length == 3) {
            //判断3个是否是相同得
            if (this.ChooseCircle[0].currentIndex == this.ChooseCircle[1].currentIndex && this.ChooseCircle[1].currentIndex == this.ChooseCircle[2].currentIndex) {
                //相同
                this.GameScene.getComponent(GameScene).CatchFish(this.ChooseCircle[0].currentIndex);
                // this.ChooseReset(true);
                // this.GameScene.getComponent(GameScene).ClickSendNetLabel();
            } else {
                //不相同
                this.ChooseReset(false);
            }
        }
    }

    /**
     * 恢复
     */
    public ChooseReset(_change: boolean) {
        for (let circle of this.ChooseCircle) {
            circle.UpdateCircle(_change ? -1 : circle.currentIndex);
        }

        this.ChooseCircle = [];
    }



}


